package com.njtoyo.taxi.admin.service.business.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njtoyo.taxi.admin.mapper.master.entity.CompanyWithdrawCanton;
import com.njtoyo.taxi.admin.mapper.master.enums.DriverWithdrawCantonState;
import com.njtoyo.taxi.admin.mapper.master.mapper.CompanyWithdrawCantonMapper;
import com.njtoyo.taxi.admin.rest.presenter.platfrom.AdminUserIdentityPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.company.AddCompanyWithdrawCantonWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.company.CompanyWithdrawCantonWrapper;
import com.njtoyo.taxi.admin.service.business.ICompanyWithdrawCantonService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 地区打款 服务实现类
 * </p>
 *
 * @author xyhang
 * @since 2024-07-23
 */
@Service
public class CompanyWithdrawCantonServiceImpl extends ServiceImpl<CompanyWithdrawCantonMapper, CompanyWithdrawCanton> implements ICompanyWithdrawCantonService {

    @Override
    public int addWithdraw(AdminUser adminUser, AddCompanyWithdrawCantonWrapper add) {
        CompanyWithdrawCanton withdrawCanton = new CompanyWithdrawCanton();
        withdrawCanton.setWithdrawTime(DateUtil.parse(add.getWithdrawTime()));
        withdrawCanton.setCantonId(add.getCantonId());
        withdrawCanton.setAmount(add.getAmount());
        withdrawCanton.setRemark(add.getRemark());
        withdrawCanton.setName(add.getName());
        withdrawCanton.setCreateTime(DateUtil.date());
        withdrawCanton.setCreateId(adminUser.getId());
        withdrawCanton.setState(DriverWithdrawCantonState.init);
        return baseMapper.insert(withdrawCanton);
    }

    @Override
    public RestResult getPage(AdminUser adminUser, CompanyWithdrawCantonWrapper wrapper) {
        AdminUserIdentityPresenter identity = (AdminUserIdentityPresenter)adminUser.getIdentity();
        switch (adminUser.getAccountType()) {
            case company:
                wrapper.setCompanyIds(identity.getCompanyIds());
                wrapper.setCantonIds(identity.getCantonIds());
                break;
            case canton:
                wrapper.setCantonIds(identity.getCantonIds());
                break;
        }
        Page<CompanyWithdrawCanton> res = baseMapper.getPage(wrapper);
        return RestResult.success(res);
    }

    @Override
    public int updateWithdraw(AdminUser adminUser, AddCompanyWithdrawCantonWrapper add) {
        CompanyWithdrawCanton withdrawCanton = new CompanyWithdrawCanton();
        withdrawCanton.setWithdrawTime(DateUtil.parse(add.getWithdrawTime()));
        withdrawCanton.setCantonId(add.getCantonId());
        withdrawCanton.setAmount(add.getAmount());
        withdrawCanton.setRemark(add.getRemark());
        withdrawCanton.setName(add.getName());
        withdrawCanton.setUpdateTime(DateUtil.date());
        withdrawCanton.setUpdateId(adminUser.getId());
        withdrawCanton.setId(add.getId());
        withdrawCanton.setState(add.getState());
        return baseMapper.updateById(withdrawCanton);
    }
}
